package gold.digger;

import java.util.ArrayList;
import java.util.List;
import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC407 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * hard 难度名不虚传，思维游戏，很有意思的题目
     * 参考动图：https://leetcode-cn.com/problems/trapping-rain-water-ii/solution/hqtu-jie-you-xian-dui-lie-tu-jie-by-hq_-1bph/
     * @return:
     */
    public enum COLOR {WHITE, GREEN, GRAY}

    class Solution {


        private class Pos {
            int height;
            int i;
            int j;

            public Pos(int height, int i, int j) {
                this.height = height;
                this.i = i;
                this.j = j;
            }
        }

        public int trapRainWater(int[][] heightMap) {
            if (heightMap.length == 0) {
                return 0;
            }
            COLOR[][] state = new COLOR[heightMap.length][heightMap[0].length];
            PriorityQueue<Pos> heap = new PriorityQueue<>(new Comparator<Pos>() {
                @Override
                public int compare(Pos o1, Pos o2) {
                    return o1.height - o2.height;
                }
            });
            for (int i = 0; i < heightMap.length; i++) {
                for (int j = 0; j < heightMap[0].length; j++) {
                    if (i == 0 || i == heightMap.length - 1 || j == 0 || j == heightMap[0].length - 1) {
                        state[i][j] = COLOR.GREEN;
                        heap.offer(new Pos(heightMap[i][j], i, j));
                    } else {
                        state[i][j] = COLOR.WHITE;
                    }
                }
            }
            Pos cur;
            int maxHeight = heap.peek().height;
            int result = 0;
            int nexti, nextj;
            int[][] direction = new int[][]{{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
            while (!heap.isEmpty()) {
                cur = heap.poll();
                maxHeight = maxHeight < cur.height ? cur.height : maxHeight;
                state[cur.i][cur.j] = COLOR.GRAY;
                for (int i = 0; i < direction.length; i++) {
                    nexti = cur.i + direction[i][0];
                    nextj = cur.j + direction[i][1];
                    if (nexti < 0 || nexti >= heightMap.length || nextj < 0 || nextj >= heightMap[0].length || state[nexti][nextj] != COLOR.WHITE) {
                        continue;
                    }
                    state[nexti][nextj] = COLOR.GREEN;
                    if (heightMap[nexti][nextj] >= maxHeight) {
                        heap.offer(new Pos(heightMap[nexti][nextj], nexti, nextj));
                    } else {
                        result += maxHeight - heightMap[nexti][nextj];
                        heap.offer(new Pos(maxHeight, nexti, nextj));
                    }
                }
            }
            return result;
        }
    }

    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC407 an = new LC407();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
